package com.lihan.deepseek;

import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Component
public class DeepSeekClient {

    private static final String API_KEY = "h9vebw_rm2Kl1TQ0beZR2h1mn-h6yWfHd-gC9QHXaG8CgN3zcRIFnqcqXH-h5Ugd3ikJmiuDLeIjSvJ5-2V1EA";
    private static final String API_URL = "https://maas-cn-southwest-2.modelarts-maas.com/v1/infers/707c01c8-517c-46ca-827a-d0b21c71b074/v1/chat/completions";


    private final OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(300, TimeUnit.SECONDS) // 连接超时
            .readTimeout(300, TimeUnit.SECONDS)    // 读取响应超时
            .writeTimeout(300, TimeUnit.SECONDS)   // 发送请求超时
            .build();
    private final ObjectMapper objectMapper = new ObjectMapper();

    public String callDeepSeek(String prompt) throws IOException {
        // 构造请求体
        RequestBody requestBody = RequestBody.create(
                objectMapper.writeValueAsString(new DeepSeekRequest(prompt)),
                MediaType.parse("application/json")
        );

        // 创建请求
        Request request = new Request.Builder()
                .url(API_URL)
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer " + API_KEY)
                .post(requestBody)
                .build();

        // 发送请求并获取响应
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code: " + response);
            }

            // 解析响应
            return objectMapper.readTree(response.body().string())
                    .path("choices").get(0)
                    .path("message").path("content").asText();
        }
    }

    // 请求参数类（根据API文档调整）
    static class DeepSeekRequest {
        private String model = "DeepSeek-V3";
        private Object[] messages;


        public DeepSeekRequest(String prompt) {
            this.messages = new Object[]{
                    new Message("user", prompt)
            };
        }

        // Getters（Jackson需要）
        public String getModel() { return model; }
        public Object[] getMessages() { return messages; }
    }

    static class Message {
        private String role;
        private String content;

        public Message(String role, String content) {
            this.role = role;
            this.content = content;
        }

        // Getters
        public String getRole() { return role; }
        public String getContent() { return content; }
    }

    public  String start(String content){
        DeepSeekClient client = new DeepSeekClient();
        String response = "";
        try {
//            Scanner scanner = new Scanner(System.in);
//            String content = scanner.nextLine();

            response = client.callDeepSeek(content);
            System.out.println("DeepSeek 响应：\n" + response);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }
}
